package com.tbs.parkingsaleapp.Http.Listenner;

import abstergo.tbs.betterasyc.AsycBase.AsycWorker;
import abstergo.tbs.betterasyc.AsycBase.IAsycBackgroundAction;
import abstergo.tbs.betterasyc.AsycBase.IAsycResultAction;
import abstergo.tbs.betterasyc.Handler.HandlerMananger;
import com.alibaba.fastjson.JSON;
import com.tbs.parkingsaleapp.Http.HttpHelper;
import com.tbs.parkingsaleapp.Http.JsonData.LocationInfo;
import com.tbs.parkingsaleapp.Http.JsonData.ParkResult;
import com.tbs.parkingsaleapp.SystemManager.Handler.CallBacks.ParkDataUpdater;
import com.tbs.parkingsaleapp.SystemManager.NetWorkSetting;
import com.tbs.parkingsaleapp.SystemManager.SystemDataMananger;
import com.tbs.parkingsaleapp.UI.HomePage.HomeFragment;

public class LocationInfosHttper implements IAsycBackgroundAction<String,LocationInfo[]>, IAsycResultAction<LocationInfo[]> {

    boolean force=false;

    public boolean isForce() {
        return force;
    }

    public void setForce(boolean force) {
        this.force = force;
    }
    public LocationInfosHttper(boolean force)
    {
        setForce(force);
    }

    @Override
    public LocationInfo[] BackGroundAction(String... params) throws Exception{
        String r= HttpHelper.Get(NetWorkSetting.LocationInfoUrl());
        return JSON.parseObject(r,LocationInfo[].class);
    }

    @Override
    public void ResultFunction(LocationInfo[] result) {
        SystemDataMananger.AppendLocationInfoCache(result);
        HomeFragment.callMapUpdate();

        for (LocationInfo ix:result
        ) {
            int id=ix.getLocation().getId();
            if(isForce())
            {
                ParkDataUpdater.Instance().NeedForUpdateNOW(id);
            }
            if(ParkDataUpdater.Instance().getUpdatedResult(id)==ParkDataUpdater.NOT_CALL)
            {
                SystemDataMananger.ClearParkFromLocation(id);
                AsycWorker<Integer, ParkResult[]> http=new  AsycWorker<Integer, ParkResult[]>(1);
                ParksFindHttpter findHttpter=  new ParksFindHttpter();
                http.setBackgroundAction(findHttpter);
                http.setResultAction(findHttpter);
                HandlerMananger.Instance().SendMsg(ParkDataUpdater.msg_CallUpdate(id));
                http.Run(id);
            }

        }
    }
}
